const app = getApp()
let qCloud = require("../../../index.js");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        dataList: [],
        deptName: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getDeptList();
    },

    /**
     * 搜索内容相关字段
     *
     * @param e
     */
    searchInput: function (e) {
        let self = this;
        self.setData({
            deptName: e.detail.value
        })
    },

    /**
     * 查询
     */
    searchButton: function () {
        let self = this;
        self.setData({
            dataList: []
        });
        self.getDeptList();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        let self = this;
        self.setData({
            dataList: []
        });
        self.getDeptList();
    },

    updateDeptTap: function (e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        })
    },

    /**长按删除*/
    deptLongTap: function (e) {
        let self = this;
        let url = qCloud.api.deleteDept + e.currentTarget.dataset.deptid;

        let params = {
            deptId: qCloud.session.getUser().organId
        };

        wx.showModal({
            content: '确定要删除部门吗？',
            success(res) {
                if (res.confirm) {
                    app.fetchApis(self, url, {}, "POST", function (e) {
                        if (e.data.code == 200) {
                            app.fetchApis(self, qCloud.api.getDeptListAll, params, "GET", function (e) {
                                if (e.data.code == 200) {
                                    self.setData({
                                        dataList: e.data.data
                                    })
                                }
                            })
                        } else {
                            wx.showToast({
                                title: e.data.msg,
                                icon: 'error'
                            })
                        }
                    })
                } else if (res.cancel) {
                    app.fetchApis(self, qCloud.api.getDeptListAll, params, "GET", function (e) {
                        if (e.data.code == 200) {
                            self.setData({
                                dataList: e.data.data
                            })
                        }
                    })
                }
            }
        })
    },

    /**
     * 获取部门列表
     */
    getDeptList: function () {
        let self = this;
        let params = {
            deptName: self.data.deptName,
            deptId: qCloud.session.getUser().organId
        };

        app.fetchApis(self, qCloud.api.getDeptListAll, params, "GET", function (e) {
            if (e.data.code == 200) {
                self.setData({
                    dataList: e.data.data
                })
            }
        })
    },

    // 新增部门
    addDepartTap(e) {
        wx.navigateTo({
            url: "/pages/enterprise/department/adds",
        })
    },


})
